package yxy.apple.util.data;

public abstract class DataContainer implements DataContainable {

	@SuppressWarnings("unchecked")
	public <T> T get(String key, T defaultValue) {
		Object data = this.get(key);
		if (data != null) {
			return (T) data;
		} else {
			return defaultValue;
		}
	}

	public Object getObject(String key, Object defaultValue) {
		Object data = this.get(key);
		if (data != null) {
			return data;
		} else {
			return defaultValue;
		}
	}

	public boolean getBoolean(String key, boolean defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof Boolean) {
			return (Boolean) data;
		} else {
			return defaultValue;
		}
	}

	public byte getByte(String key, byte defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof Byte) {
			return (Byte) data;
		} else {
			return defaultValue;
		}
	}

	public short getShort(String key, short defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof Short) {
			return (Short) data;
		} else {
			return defaultValue;
		}
	}

	public int getInt(String key, int defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof Integer) {
			return (Integer) data;
		} else {
			return defaultValue;
		}
	}

	public long getLong(String key, long defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof Long) {
			return (Long) data;
		} else {
			return defaultValue;
		}
	}

	public float getFloat(String key, float defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof Float) {
			return (Float) data;
		} else {
			return defaultValue;
		}
	}

	public double getDouble(String key, double defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof Double) {
			return (Double) data;
		} else {
			return defaultValue;
		}
	}

	public String getString(String key, String defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof String) {
			return (String) data;
		} else {
			return defaultValue;
		}
	}

	@Override
	public byte[] getBytes(String key, byte[] defaultValue) {
		Object data = this.getObject(key, null);
		if (data != null && data instanceof byte[]) {
			return (byte[]) data;
		} else {
			return defaultValue;
		}
	}
}
